package com.example.service;
import com.example.POJO.Account;
import com.example.mapper.AccountMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class AccountService {

    // get all accounts
    public List<Account> getAllAccounts() throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);
        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        List<Account> accounts=accountMapper.getAllAccounts();
        sqlSession.close();
        System.out.println(accounts);
        return accounts;
    }

    public void insertAccount(Account account) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        accountMapper.insertAccount(account);

        sqlSession.close();
    }

    public String getPasswordByPhone(String phone) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        String password=accountMapper.getPasswordByPhone(phone);

        sqlSession.close();
        return password;
    }

    public boolean phoneIsExit(String phone) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        Integer id=accountMapper.getIdByPhone(phone);

        sqlSession.close();
        if(id!=null){
            return true;
        }else {
            return false;
        }
    }

    public Integer getIdByPhone(String phone) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        Integer id=accountMapper.getIdByPhone(phone);

        sqlSession.close();
        return id;
    }

    public void updatePassword(Account account) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        accountMapper.updatePassword(account);

        sqlSession.close();
    }

    public boolean phoneIsExitUnity(String phone) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);

        Integer id=accountMapper.getIdByPhoneUnity(phone);
        sqlSession.close();
        if(id!=null){
            return true;
        }else {
            return false;
        }
    }

    public void insertUnityAccount(Account account) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        accountMapper.insertUnityAccount(account);

        sqlSession.close();
    }

    public boolean boolPasswordUnity(Account account) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        String passwordSQL=accountMapper.getPasswordByPhoneUnity(account);
        sqlSession.close();
        if(passwordSQL.equals(account.getPassword())){
            return true;
        }else {
            return false;
        }
    }

    public void updatePasswordUnity(Account account) throws IOException {

        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
        accountMapper.updatePasswirdUnity(account);
        sqlSession.close();
    }
}
